package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ngc.mall.domain.LotteryItemDomain;
import com.ngc.mall.domain.LotteryPrizeDomain;
import com.ngc.mall.dto.LotteryItemDTO;
import com.ngc.mall.mapper.LotteryItemMapper;
import com.ngc.mall.service.ILotteryItemService;
import com.ngc.mall.service.ILotteryPrizeService;
import com.ngc.mall.vo.LotteryItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * 奖项item 实现类
 * @author: chenxiaolin
 * @date 2023/10/14 14:15
**/
@Service
public class LotteryItemServiceImpl extends ServiceImpl<LotteryItemMapper, LotteryItemDomain> implements ILotteryItemService {

    @Autowired
    private ILotteryPrizeService iLotteryPrizeService;

    @Override
    public LotteryItemVO findById(Long id){
        LotteryItemDomain lotteryItemDomain=super.getById(id);
        LotteryPrizeDomain lotteryPrizeDomain=iLotteryPrizeService.getById(lotteryItemDomain.getPrizeId());
        LotteryItemVO lotteryItemVO=LotteryItemVO.builder().percent(lotteryItemDomain.getPercent()).lotteryPrizeDomain(lotteryPrizeDomain).itemName(lotteryItemDomain.getItemName()).id(id).build();
        return lotteryItemVO;
    }

    @Override
    @Transactional
    public Boolean updateById(Long id, LotteryItemDTO dto){
        super.update(Wrappers.<LotteryItemDomain>lambdaUpdate().eq(LotteryItemDomain::getId,id).set(LotteryItemDomain::getItemName,dto.getItemName()).set(LotteryItemDomain::getPercent,dto.getPercent()));
        return iLotteryPrizeService.update(dto.getPrize(),Wrappers.<LotteryPrizeDomain>lambdaUpdate().eq(LotteryPrizeDomain::getId,dto.getPrize().getId()));
    }

    @Override
    @Transactional
    public Boolean removeById(Long id){
        LotteryItemDomain domain=super.getById(id);
        iLotteryPrizeService.removeById(domain.getPrizeId());
        return super.removeById(id);
    }
}
